package com.lt.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lt.crm.pojo.SysRole;
import com.lt.crm.pojo.SysUser;
import com.lt.crm.service.SysRoleService;
import com.lt.crm.service.SysUserService;
import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysRoleService sysRoleService;

    /**
     * 查询
     *
     * @param usrName
     * @param model
     * @param roleId
     * @param pageIndex
     * @return
     */
    @RequestMapping("/list")
    public String findUsers(String usrName, Model model,
                            @RequestParam(defaultValue = "0", required = false) Integer roleId,
                            @RequestParam(required = false, defaultValue = "1") Integer pageIndex) {
       // Sort sort = new Sort(Sort.Direction.ASC, "usrId");
//        Sort sort = Sort.by(Sort.Direction.ASC, "id");
//        Pageable pageable = PageRequest.of(pageIndex - 1, 5, sort);
//        Page<SysUser> userPager = sysUserService.findUsers(usrName, roleId, pageable);

        // 创建分页对象
        Page<SysUser> page = new Page<>(pageIndex, 5); // pageIndex从1开始，MyBatis-Plus会自动减1

        // 执行分页查询
        Page<SysUser> users = sysUserService.findUsers(usrName, roleId, page);
        System.out.println(users.getRecords());
        //System.out.println(users.getRecords()+",,,,,,,,,,,,,,,,,,,,,,,,,,,,");
        model.addAttribute("userPager", users);
        model.addAttribute("userName", usrName);
        model.addAttribute("roleId", roleId);
        return "/user/list";
    }




    /**
     * 删除
     *
     * @param usrId
     * @return
     */
    @RequestMapping("/del/{usrId}")
    @ResponseBody
    public Map del(@PathVariable Integer usrId) {
        Map<String, String> map = new HashMap<>();
        try {
            sysUserService.removeById(usrId);
            map.put("delResult", "true");
        } catch (RuntimeException e) {
            map.put("delResult", "false");
        }
        return map;
    }
    @RequestMapping("/backdel/{usrId}")
    @ResponseBody
    public Map backdel(@PathVariable Integer usrId) {
        Map<String, String> map = new HashMap<>();
        try {
            sysUserService.deletebyUsrId(usrId);
            map.put("delResult", "true");
        } catch (RuntimeException e) {
            map.put("delResult", "false");
        }
        return map;
    }

    /**
     * 跳转至添加页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String toAdd(Model model) {
        List<SysRole> roles = sysRoleService.list();
        model.addAttribute("roles", roles);
        return "user/add";
    }

    /**
     * 新增和修改
     *
     * @param user
     * @return
     */
    @RequestMapping("/save")
    public String save(SysUser user) {
        System.out.println(user);
        user.setUsrPassword(sysUserService.encryptPassword(user.getUsrPassword()));
        sysUserService.saveOrUpdate(user);
        return "redirect:/user/list";
    }

    /**
     * 跳转至修改页面
     *
     * @param usrId
     * @param model
     * @return
     */
    //@RequiresPermissions("用户编辑")
    @RequestMapping("/toEdit")
    public String edit(@RequestParam Integer usrId, Model model) {
        SysUser user = sysUserService.getById(usrId);
        SysRole sysRole =sysRoleService.getById(user.getUsrRoleId());
        user.setRole(sysRole);
        List<SysRole> roles = sysRoleService.list();
        model.addAttribute("roles", roles);
        model.addAttribute("user", user);

        return "user/edit";
    }

    @RequestMapping("/back/{usrId}")
    @ResponseBody
    public Map back(@PathVariable Integer usrId) {
        Map<String, String> map = new HashMap<>();
        try {

           int i= sysUserService.updatebyusrId(usrId);
            map.put("delResult", "true");
        } catch (RuntimeException e) {
            map.put("delResult", "false");
        }
        return map;
    }


    @RequestMapping("/backlist")
    public String toBack(String usrName, Model model,
                         @RequestParam(defaultValue = "0", required = false) Integer roleId,
                         @RequestParam(required = false, defaultValue = "1") Integer pageIndex) {

        // 创建分页对象
        Page<SysUser> page = new Page<>(pageIndex, 5); // pageIndex从1开始，MyBatis-Plus会自动减1

        // 执行分页查询
        Page<SysUser> users = sysUserService.findUsers2(usrName, roleId, page);
        System.out.println(users.getRecords());
        //System.out.println(users.getRecords()+",,,,,,,,,,,,,,,,,,,,,,,,,,,,");
        model.addAttribute("userPager", users);
        model.addAttribute("userName", usrName);
        model.addAttribute("roleId", roleId);
        return "user/back";
    }




}
